#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QString>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    fTimer = new QTimer(this);
    fTimer->stop();
    fTimer->setInterval(1000) ;//设置定时周期，单位：毫秒
    connect(fTimer,SIGNAL(timeout()),this,SLOT(on_timer_timeout()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_timer_timeout()
{
    //定时器中断响
    QTime curTime=QTime::currentTime(); //获取当前时间
    ui->lcdNumber->display (curTime.hour()); //显示小时
    ui->lcdNumber_2->display (curTime.minute()); //显示分钟
    ui->lcdNumber_3->display(curTime.second()); //显示秒
//    int va=ui->progressBar->value();
//    va++;
//    if (va>100)
//        va=0;
//    ui->progressBar->setValue(va);
}

// 开始定时器
void MainWindow::on_pushButton_2_clicked()
{
    fTimer->start();

    beginTime = QTime::currentTime() ; //计时器开始工作
    ui->pushButton_2->setEnabled(false);
    ui->pushButton_6->setEnabled(true);
    ui->pushButton_7->setEnabled(false);

}

// 停止定时器
void MainWindow::on_pushButton_6_clicked()
{
    fTimer->stop();
    ui->lcdNumber->display (0); //显示小时
    ui->lcdNumber_2->display (0); //显示分钟
    ui->lcdNumber_3->display(0); //显示秒

    endTime = QTime::currentTime();

    int tmMsec=beginTime.msecsTo(endTime) ;//毫秒数
    qDebug() << tmMsec ;
    int ms = tmMsec % 1000;
    qDebug() << ms ;
    int sec = tmMsec / 1000;
    qDebug() << sec ;
    QString str = QString::asprintf ("%d.%d", sec, ms);
    ui->label_5->setText(str);

    ui->pushButton_2->setEnabled(true);
    ui->pushButton_6->setEnabled(false);
    ui->pushButton_7->setEnabled(true);
}

// 关闭
void MainWindow::on_pushButton_9_clicked()
{
    this->close();
}

// 设置定时间隔
void MainWindow::on_pushButton_7_clicked()
{
    int value = ui->spinBox->value();
    if(value == 0){
        fTimer->setInterval(1000);
    }else{
        fTimer->setInterval(value * 1000);
    }
}

void MainWindow::on_spinBox_valueChanged(int arg1)
{

}

// 读取当前时间
void MainWindow::on_pushButton_clicked()
{
    QTime now = QTime::currentTime();
    int hour = now.hour();
    int min = now.minute();
    int sec = now.second();
    QString timeStr = QString("当前时间 %1: %2: %3") //
            .arg(hour).arg(min).arg(sec); //
    ui->label->setText(timeStr);//
}

void MainWindow::on_calendarWidget_selectionChanged()
{
    QDate selectDate = ui->calendarWidget->selectedDate();
    int localYear = selectDate.year();
    int localMonth = selectDate.month();
    int localDay = selectDate.day();

    QString timeStr = QString("当前日期 %1: %2: %3") //
            .arg(localYear).arg(localMonth).arg(localDay); //


    ui->lineEdit_2->setText(timeStr);
}

// 选择日期
void MainWindow::on_pushButton_8_clicked()
{
    QDate selectDate = ui->calendarWidget->selectedDate();

    int localYear = selectDate.year();
    int localMonth = selectDate.month();
    int localDay = selectDate.day();

    QString timeStr = QString("当前日期 %1: %2: %3") //
            .arg(localYear).arg(localMonth).arg(localDay); //


    ui->lineEdit_2->setText(timeStr);

}
